// pages/calendar/calendar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    startTime: {
      type: String,
      value: '',
    },
    endTime: {
      type: String,
      value: '',
    },
    datesSign: {
      type: Array,
      value: []
    },
    estimateSign: {
      type: Array,
      value: []
    },
    startDate: {
      type: Object,
      value: {

      }
    }


  },

  /**
   * 组件的初始数据
   */
  data: {
    color: '#45B1ED',
    startX:0,
    endX:0,
    startY:0,
    endY:0,
    //移动量
    moveto:0,
    //是否滑动？
    istouch:false,
    //上下月份显示
    isshow:false,
    //滑动未完成时禁止滑动
    ishd:true,

    //滑动按下的时间
    touchstarttime:0,
    
    //判断手势第一次左右滑动还是上下
    isleftorright:false,
    //是否左右滑动
    iszyhd:false,

  },
  onPageScroll(e) {
    console.log(e.scrollTop)
  },

  /**
   * 组件的方法列表
   */
  methods: {
    lastdatafun() {
      let that=this
      if (this.data.nowDay.month == 12) {
        this.data.nowDay.month = 1
        this.data.nowDay.year = this.data.nowDay.year + 1
      } else {
        this.data.nowDay.month = this.data.nowDay.month + 1
      }
      // console.log(this.data.nowDay)
      this.getdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)

      this.getlastdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)
      this.getnextdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)
      // that.setData({
      //   isshow:false,
      //   istouch:false,
      //   moveto:0+'rpx'
      // })
      // setTimeout(()=>{
      //   that.setData({
      //     moveto:0+'rpx'
      //   })
      // },10)
    },
    nextdatafun() {
      let that=this
      if (this.data.nowDay.month == 1) {
        this.data.nowDay.month = 12
        this.data.nowDay.year = this.data.nowDay.year - 1
      } else {
        this.data.nowDay.month = this.data.nowDay.month - 1
      }
      // console.log(this.data.nowDay)
      this.getdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)
      this.getlastdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)
      this.getnextdates(this.data.nowDay.year, this.data.nowDay.month, this.data.nowDay.day)
      
      // setTimeout(()=>{
      //   that.setData({
      //     moveto:0+'rpx'
      //   })
      // },10)
    },
    // 当月天数
    getdates(year, month, day, now) {
      // year=2022
      // month=1
      let datalists = new Date(year, month, 0).getDate()
      let dataarr = []
      let lastdataarr = []
      // console.log('星期',new Date().getDay(),'年月日',year,month,day)

      let week = new Date(year + '/' + month + '/' + 1).getDay() == 0 ? 7 : new Date(year + '/' + month + '/' + 1).getDay()


      let nextdatas = null

      if (month == 1) {
        nextdatas = new Date(year - 1, 13, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year - 1,
            M: 12,
            D: nextdatas - (week - index - 2),
            DT: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      } else {
        nextdatas = new Date(year, month - 1, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year,
            M: month - 1,
            D: nextdatas - (week - index - 2),
            DT: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      }

      // console.log('当月星期',new Date(year,month,1))
      for (let index = 0; index < datalists; index++) {
        let datas = {
          Y: year,
          M: month,
          D: index + 1,
          DT: new Date(year + '/' + month + '/' + parseInt(index + 1)).getTime(),
          week: new Date(year + '/' + month + '/' + parseInt(index + 1)).getDay()
        }
        // console.log('sda',new Date(year+'/'+month+'/'+))
        dataarr[index] = datas

      }

      this.setData({
        datalist: dataarr,
        lastdatalist: lastdataarr
      })
      let monthtexts = ''
      switch (this.data.nowDay.month) {
        case 1:
          monthtexts = '一月'
          break;
        case 2:
          monthtexts = '二月'
          break;
        case 3:
          monthtexts = '三月'
          break;
        case 4:
          monthtexts = '四月'
          break;
        case 5:
          monthtexts = '五月'
          break;
        case 6:
          monthtexts = '六月'
          break;
        case 7:
          monthtexts = '七月'
          break;
        case 8:
          monthtexts = '八月'
          break;
        case 9:
          monthtexts = '九月'
          break;
        case 10:
          monthtexts = '十月'
          break;
        case 11:
          monthtexts = '十一月'
          break;
        case 12:
          monthtexts = '十二月'
          break;

        default:
          break;
      }
      this.setData({
        monthtext: monthtexts
      })
    },
    getlastdates(year, month, day, now) {
      // year=2022
      // month=1
      if (month==1) {
        year=year-1
        month=12
      }else{
        month=month-1
      }
      
      let datalists = new Date(year, month, 0).getDate()
      let dataarr = []
      let lastdataarr = []
      // console.log('星期',new Date().getDay(),'年月日',year,month,day)

      let week = new Date(year + '/' + month + '/' + 1).getDay() == 0 ? 7 : new Date(year + '/' + month + '/' + 1).getDay()


      let nextdatas = null

      if (month == 1) {
        nextdatas = new Date(year - 1, 13, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year - 1,
            M: 12,
            D: nextdatas - (week - index - 2),
            DT: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      } else {
        nextdatas = new Date(year, month - 1, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year,
            M: month - 1,
            D: nextdatas - (week - index - 2),
            DT: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      }

      // console.log('当月星期',new Date(year,month,1))
      for (let index = 0; index < datalists; index++) {
        let datas = {
          Y: year,
          M: month,
          D: index + 1,
          DT: new Date(year + '/' + month + '/' + parseInt(index + 1)).getTime(),
          week: new Date(year + '/' + month + '/' + parseInt(index + 1)).getDay()
        }
        // console.log('sda',new Date(year+'/'+month+'/'+))
        dataarr[index] = datas

      }

      this.setData({
        datalistlast: dataarr,
        lastdatalistlast: lastdataarr,
        isshow:false,
        istouch:false,
        moveto:0+'rpx',
        ishd:true,
      })
      // that.setData({
        
      // })
      // let monthtexts = ''
      // switch (this.data.nowDay.month) {
      //   case 1:
      //     monthtexts = '一月'
      //     break;
      //   case 2:
      //     monthtexts = '二月'
      //     break;
      //   case 3:
      //     monthtexts = '三月'
      //     break;
      //   case 4:
      //     monthtexts = '四月'
      //     break;
      //   case 5:
      //     monthtexts = '五月'
      //     break;
      //   case 6:
      //     monthtexts = '六月'
      //     break;
      //   case 7:
      //     monthtexts = '七月'
      //     break;
      //   case 8:
      //     monthtexts = '八月'
      //     break;
      //   case 9:
      //     monthtexts = '九月'
      //     break;
      //   case 10:
      //     monthtexts = '十月'
      //     break;
      //   case 11:
      //     monthtexts = '十一月'
      //     break;
      //   case 12:
      //     monthtexts = '十二月'
      //     break;

      //   default:
      //     break;
      // }
      // this.setData({
      //   monthtext: monthtexts
      // })
    },
    getnextdates(year, month, day, now) {
      // year=2022
      // month=1
      if (month==12) {
        year=year+1
        month=1
      }else{
        month=month+1
      }
      let datalists = new Date(year, month, 0).getDate()
      let dataarr = []
      let lastdataarr = []
      // console.log('星期',new Date().getDay(),'年月日',year,month,day)

      let week = new Date(year + '/' + month + '/' + 1).getDay() == 0 ? 7 : new Date(year + '/' + month + '/' + 1).getDay()


      let nextdatas = null

      if (month == 1) {
        nextdatas = new Date(year - 1, 13, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year - 1,
            M: 12,
            D: nextdatas - (week - index - 2),
            DT: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(parseInt(year - 1) + '/' + '12' + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      } else {
        nextdatas = new Date(year, month - 1, 0).getDate()
        for (let index = 0; index < week - 1; index++) {
          let lastdatas = {
            Y: year,
            M: month - 1,
            D: nextdatas - (week - index - 2),
            DT: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getTime(),
            week: new Date(year + '/' + parseInt(month - 1) + '/' + parseInt(nextdatas - (week - index - 2))).getDay()
          }
          lastdataarr[index] = lastdatas
        }
      }

      // console.log('当月星期',new Date(year,month,1))
      for (let index = 0; index < datalists; index++) {
        let datas = {
          Y: year,
          M: month,
          D: index + 1,
          DT: new Date(year + '/' + month + '/' + parseInt(index + 1)).getTime(),
          week: new Date(year + '/' + month + '/' + parseInt(index + 1)).getDay()
        }
        // console.log('sda',new Date(year+'/'+month+'/'+))
        dataarr[index] = datas

      }

      this.setData({
        datalistnext: dataarr,
        lastdatalistnext: lastdataarr,
        isshow:false,
        istouch:false,
        moveto:0+'rpx',
        ishd:true,
      })
      // let monthtexts = ''
      // switch (this.data.nowDay.month) {
      //   case 1:
      //     monthtexts = '一月'
      //     break;
      //   case 2:
      //     monthtexts = '二月'
      //     break;
      //   case 3:
      //     monthtexts = '三月'
      //     break;
      //   case 4:
      //     monthtexts = '四月'
      //     break;
      //   case 5:
      //     monthtexts = '五月'
      //     break;
      //   case 6:
      //     monthtexts = '六月'
      //     break;
      //   case 7:
      //     monthtexts = '七月'
      //     break;
      //   case 8:
      //     monthtexts = '八月'
      //     break;
      //   case 9:
      //     monthtexts = '九月'
      //     break;
      //   case 10:
      //     monthtexts = '十月'
      //     break;
      //   case 11:
      //     monthtexts = '十一月'
      //     break;
      //   case 12:
      //     monthtexts = '十二月'
      //     break;

      //   default:
      //     break;
      // }
      // this.setData({
      //   monthtext: monthtexts
      // })
    },
    // 开始和结束时间
    getrange(start, end) {
      if (start == '' || end == '') {
        return
      }
      let startday = start.replace(/-/g, "/")
      let endday = end.replace(/-/g, "/")
      let startselect = new Date(startday)
      let endselect = new Date(endday)
      this.setData({
        startdays: {
          year: startselect.getFullYear(),
          month: startselect.getMonth() + 1,
          day: startselect.getDate(),
          datetime: startselect.getTime()
        },
        enddays: {
          year: endselect.getFullYear(),
          month: endselect.getMonth() + 1,
          day: endselect.getDate(),
          datetime: endselect.getTime()
        }
      })

    },
    // 标记点
    getdatasign(dataarr) {
      let signdataarr = []
      dataarr.forEach(element => {
        let elday = new Date(element.data.replace(/-/g, "/"))

        let signlist = {
          year: elday.getFullYear(),
          month: elday.getMonth() + 1,
          day: elday.getDate(),
          color: element.color
        }
        signdataarr.push(signlist)
      });

      if(this.data.signlist){
        this.setData({
          signlist: this.data.signlist.concat(signdataarr)
        })
      }else{
        this.setData({
          signlist: signdataarr
        })
      }
      
     
      console.log('signlist',this.data.signlist)
    },
    // 完工标记的
    getwgsign(dataarr) {
      console.log('dataarr',dataarr)
      // let signdataarr = []
      // dataarr.forEach(element => {
        let elday = new Date(dataarr.date.replace(/-/g, "/"))

        let signlist = [{
          year: elday.getFullYear(),
          month: elday.getMonth() + 1,
          day: elday.getDate(),
          color: dataarr.color
        }]
        console.log('sda1',signlist)
        // signdataarr.push(signlist)
      // });
      if(this.data.signlist){
        this.setData({
          signlist: this.data.signlist.concat(signlist)
        })
      }else{
        this.setData({
          signlist: signlist
        })
      }
      
      console.log('sda2',this.data.signlist)
    },
    getyjsign(dataarr) {
      let signdataarr = []
      dataarr.forEach(element => {
        let elday = new Date(element.data.replace(/-/g, "/"))

        let signlist = {
          year: elday.getFullYear(),
          month: elday.getMonth() + 1,
          day: elday.getDate(),
          color: element.color
        }
        signdataarr.push(signlist)
      });
      this.setData({
        yjsignlist: signdataarr
      })
    },
    // 渲染
    Rendering() {
      //    开始日期
      let now = this.data.startTime ? new Date(this.data.startTime) : new Date()

      // let nextmonth=
      // 当前日期
      let endday = new Date()
      let selectDay = {
        year: now.getFullYear(),
        month: now.getMonth() + 1,
        day: now.getDate()
      }
      this.setData({
        nowDay: {
          year: now.getFullYear(),
          month: now.getMonth() + 1,
          day: now.getDate()
        }

      })
      this.setData({
        isselectDay: {
          year: endday.getFullYear(),
          month: endday.getMonth() + 1,
          day: endday.getDate()
        }
      })
      this.getdates(selectDay.year, selectDay.month, selectDay.day, now)
      this.getlastdates(selectDay.year, selectDay.month, selectDay.day, now)
      this.getnextdates(selectDay.year, selectDay.month, selectDay.day, now)
      this.getrange(this.data.startTime, this.data.endTime)
      this.getdatasign(this.data.datesSign)
      this.getyjsign(this.data.estimateSign)
      this.getwgsign(this.data.startDate)
    },
    //手指触摸动作开始
  touchStart(e){
    // let touchstarttime=new Date().getTime()
    // this.data.touchstarttime=touchstarttime
   
    // if (this.data.ishd==false) {
    //   return
    // } 
    console.log(e.touches[0].pageX)
    this.setData({
      istouch:false,
      isshow:true,
      ishd:false,
    })
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.startX=sx
    this.data.startY=sy
  },
  touchMove(e){
   
    let that=this
    
    var xOffset = e.touches[0].pageX - that.data.startX;
      var yOffset = e.touches[0].pageY - that.data.startY;

      if (that.data.touchstarttime>new Date().getTime()) {
 
      }
    if(this.data.isleftorright==false){

if (Math.abs(xOffset) >= Math.abs(yOffset)) {
        //左右滑动
        //③条件3（判断偏移量的正负）
        
          this.setData({
            isleftorright:true,
            iszyhd:true})
          console.log('this.data.isleftorright2',this.data.isleftorright)

       
       
      }

      if (Math.abs(xOffset) <= Math.abs(yOffset)) {
        //上下滑动
        //③条件3（判断偏移量的正负）
        console.log('this.data.isleftorright4',this.data.isleftorright)
        
          this.setData({
            isleftorright:true,
            iszyhd:false
          })
       
      }
    }
      if (this.data.iszyhd==true) {
        console.log(e.touches[0].pageX,that.data.startX)
        this.setData({
          moveto:e.touches[0].pageX-that.data.startX+'px'
        })
      }
    return

    if (Math.abs(angle) < 30 && touchMoveX < that.data.startX) {
      // 向左滑动
      this.setData({
        moveto:e.touches[0].pageX-that.data.startX+'px'
      })
 };
 if (Math.abs(angle) < 30 && touchMoveX > that.data.startX ) {
      // 向右滑动
      this.setData({
        moveto:e.touches[0].pageX-that.data.startX+'px'
      })
 }
    
  },
  touchEnd(e){
    if (this.data.iszyhd==false) {
      this.setData({isleftorright:false,
        iszyhd:false})
    } else{
    this.setData({isleftorright:false,
      iszyhd:false})
    // return
    let that=this
    var xOffset = that.data.endX - that.data.startX;
    var yOffset = that.data.endY - that.data.startY;
    // Math.abs(xOffset) >= Math.abs(yOffset) &&
    
    
    if ( e.changedTouches[0].clientX-this.data.startX>50) {
      this.setData({
        istouch:true,
        moveto:666+'rpx'
      })
      
      setTimeout(()=>{
      that.nextdatafun()
    },200)
    // Math.abs(xOffset) >= Math.abs(yOffset)&&
    }else if (e.changedTouches[0].clientX-this.data.startX<-50) {
      this.setData({
        istouch:true,
        moveto:-666+'rpx'
      })
      setTimeout(()=>{
      that.lastdatafun()
    
    },200)
    }else{
      this.setData({
        
        istouch:true,
        moveto:0+'rpx',
        ishd:false
      })
    }
  }
  },

  //计算滑动角度
  angle(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y;
    //返回角度 Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  },
 
  lifetimes: {
    ready() {
      
    }
  },





  observers: {

  }

})